package com.tuhu.dai.domain.auth.service;


import com.google.common.cache.Cache;
import com.tuhu.dai.domain.auth.adapter.port.ILoginPort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: DaiTao
 * @description:
 * @create: 2025-08-09 17:52
 **/
@Service
@Slf4j
public class WeiXinServiceImpl implements ILoginService {

    @Resource
    private ILoginPort loginPort;

    @Resource
    private Cache<String, String> weixinAccessToken;


    @Override
    public String createQrCodeTicket() throws Exception {
        return loginPort.createQrCodeTicket();
    }

    @Override
    public String checkLogin(String ticket) {
        return weixinAccessToken.getIfPresent(ticket);
    }

    @Override
    public void saveLoginState(String ticket, String openid) throws IOException{

        // 绑定ticket-openid
        weixinAccessToken.put(ticket, openid);

        // 发送模版消息
        loginPort.sendLoginTemplate(openid);
    }


}
